package com.yjb.controller;


import com.yjb.utils.RandomizationUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;


/**
 *
 */
@Controller
@RequestMapping("/sms")
public class SmsVCController {

    /**
     * 生成验证码
     */
    @GetMapping("/generateVC")
    @ResponseBody
    public String generateVC(){
        // 生成验证码
        // 4位短信验证码
        Integer verifyCode = RandomizationUtil.generateValidateCode(4);
        // 存入redis
        Jedis jedis = new Jedis("localhost", 6379);
        jedis.set("verifyCodeByUserId",verifyCode.toString());

        return verifyCode.toString();
    }

    /**
     *  校验验证码
     */
    @GetMapping
    @ResponseBody
    public Boolean checkVerifyCode(@RequestParam("verifyCode")String verifyCode){
        //参数校验
        if (StringUtils.isEmpty(verifyCode)){
            return false;
        }
        //redis获取验证码
        Jedis jedis = new Jedis("localhost", 6379);
        String verifyCodeByUserId = jedis.get("verifyCodeByUserId");
        if (verifyCodeByUserId.equals(verifyCode)){
            return true;
        }
        return false;
    }
}
